package com.hxzy.config.security.authentication;

import com.hxzy.common.constans.RedisConstans;
import com.hxzy.common.util.ServletUtils;
import com.hxzy.common.vo.R;
import com.hxzy.config.RedisTemplateConfig;
import com.hxzy.token.TokenService;
import com.hxzy.vo.AdminLoginVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义spring security登出成功
 *
 * @author tonneyyy
 */
@Component
public class NewBeeLogoutSuccessHandler implements LogoutSuccessHandler {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        AdminLoginVO adminLoginVO = this.tokenService.getAdminLoginUser(request, response);
        if(adminLoginVO!=null){
             // 删除令牌
            String redisKey= RedisConstans.getRedisKey(RedisConstans.TOKEN_KEY, adminLoginVO.getUuid());
            this.redisTemplate.delete(redisKey);
        }
        R r = R.ok();
        r.setMsg("登出成功");
        ServletUtils.renderString(response,r);
    }
}
